package org.films.web;

import java.lang.Long;
import java.lang.String;
import javax.validation.Valid;
import org.films.domain.Category;
import org.films.domain.Film;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

privileged aspect CategoryController_Roo_Controller {
    
    @RequestMapping(value = "/category", method = RequestMethod.POST)    
    public String CategoryController.create(@Valid Category category, BindingResult result, ModelMap modelMap) {    
        if (category == null) throw new IllegalArgumentException("A category is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("categorys", Category.findAllCategorys());            
            modelMap.addAttribute("films", Film.findAllFilms());            
            return "category/create";            
        }        
        category.persist();        
        return "redirect:/category/" + category.getId();        
    }    
    
    @RequestMapping(value = "/category/form", method = RequestMethod.GET)    
    public String CategoryController.createForm(ModelMap modelMap) {    
        modelMap.addAttribute("category", new Category());        
        modelMap.addAttribute("categorys", Category.findAllCategorys());        
        modelMap.addAttribute("films", Film.findAllFilms());        
        return "category/create";        
    }    
    
    @RequestMapping(value = "/category/{id}", method = RequestMethod.GET)    
    public String CategoryController.show(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("category", Category.findCategory(id));        
        return "category/show";        
    }    
    
    @RequestMapping(value = "/category", method = RequestMethod.GET)    
    public String CategoryController.list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {    
        if (page != null || size != null) {        
            int sizeNo = size == null ? 10 : size.intValue();            
            modelMap.addAttribute("categorys", Category.findCategoryEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));            
            float nrOfPages = (float) Category.countCategorys() / sizeNo;            
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));            
        } else {        
            modelMap.addAttribute("categorys", Category.findAllCategorys());            
        }        
        return "category/list";        
    }    
    
    @RequestMapping(method = RequestMethod.PUT)    
    public String CategoryController.update(@Valid Category category, BindingResult result, ModelMap modelMap) {    
        if (category == null) throw new IllegalArgumentException("A category is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("categorys", Category.findAllCategorys());            
            modelMap.addAttribute("films", Film.findAllFilms());            
            return "category/update";            
        }        
        category.merge();        
        return "redirect:/category/" + category.getId();        
    }    
    
    @RequestMapping(value = "/category/{id}/form", method = RequestMethod.GET)    
    public String CategoryController.updateForm(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("category", Category.findCategory(id));        
        modelMap.addAttribute("categorys", Category.findAllCategorys());        
        modelMap.addAttribute("films", Film.findAllFilms());        
        return "category/update";        
    }    
    
    @RequestMapping(value = "/category/{id}", method = RequestMethod.DELETE)    
    public String CategoryController.delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        Category.findCategory(id).remove();        
        return "redirect:/category?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());        
    }    
    
    @RequestMapping(value = "find/ByNameEquals/form", method = RequestMethod.GET)    
    public String CategoryController.findCategorysByNameEqualsForm(ModelMap modelMap) {    
        return "category/findCategorysByNameEquals";        
    }    
    
    @RequestMapping(value = "find/ByNameEquals", method = RequestMethod.GET)    
    public String CategoryController.findCategorysByNameEquals(@RequestParam("name") String name, ModelMap modelMap) {    
        if (name == null || name.length() == 0) throw new IllegalArgumentException("A Name is required.");        
        modelMap.addAttribute("categorys", Category.findCategorysByNameEquals(name).getResultList());        
        return "category/list";        
    }   
    
    @RequestMapping(value = "/category/category_films")    
    public String CategoryController.listCategoryFilms(ModelMap modelMap) {                 
        modelMap.addAttribute("categoryFilms", Category.listCategoryFilms());        
        return "category/categoryFilms";        
    } 
    
}
